/***************************************
 ***                                 ***
 ***                                 ***
 ***                                 ***
 ***************************************/
 
/** InputForm [静态类页面 获取 / 渲染 数据] */
let InputForm = (function(){
    // 类的内部方法
    var PrivateClass = {
        description: 'This Method For InputForm Private Methods',
        getElement: function (Name){
            var MainCtrl = false;
            if(Name.charAt(0) === '.'){
                MainCtrl = document.getElementsByClassName(Name.substr(1));
            }else if(Name.charAt(0) === '#'){
                MainCtrl = document.getElementById(Name.substr(1));
            }else{
                MainCtrl = document.getElementsByTagName(Name);
            }

            return MainCtrl;
        },
        /** [getDocElem 获取 Document 元素对象] */
        getDocElement: function (MainCtrl){
            if(MainCtrl === null || typeof MainCtrl === 'string'){
                MainCtrl = MainCtrl === null ? document : this.getElement(MainCtrl);
            }
            

            if(MainCtrl instanceof jQuery){
                return MainCtrl.get();
            }else if(MainCtrl instanceof HTMLElement || MainCtrl instanceof Document){
                return [MainCtrl];
            }else if(MainCtrl instanceof HTMLCollection){
                return Array.prototype.slice.call(MainCtrl ,0);
            }
        },
        /** [getValElement 获取目标控件元素下的所有 InputValue] */
        getValElement: function (MainCtrl){
            MainCtrl = this.getDocElement(MainCtrl);
            
            var ValElements = new Array();
            if(MainCtrl instanceof HTMLElement || MainCtrl instanceof Document){
                ValElements = MainCtrl.querySelectorAll('input,select');
            }else if(MainCtrl instanceof Array || MainCtrl instanceof HTMLCollection){
                for(var i = 0;i < MainCtrl.length;i++){
                    ValElements = ValElements.concat(Array.prototype.slice.call(MainCtrl[i].querySelectorAll('input,select') ,0));
                }
            }
            
            return ValElements;
        },
        /** [rendering 渲染 Input Value] */
        rendering: function (MainCtrl ,DemoElem ,data){
            for(var i=0;i < data.length;i ++){
                var ElemGroup = DemoElem === null ? MainCtrl : DemoElem.cloneNode(true);
                var contentElems = ElemGroup.querySelectorAll('div,span,p,input,img,select');

                var valueObj = data[i];
                for(var j=0;j < contentElems.length;j++){
                    var inputElem = contentElems[j];

                    var key = inputElem.getAttribute('name');
                    key = key !== null ? (valueObj[key] ? key : (valueObj[key.replace(/-/g ,'_')] ? key.replace(/-/g ,'_') : false)) : false;
                    if(key !== false){
                        var is_exists = inputElem.getAttribute('is-exists');
                        var is_set = inputElem.getAttribute('no-set') == undefined ? true : false;
                        if(inputElem.tagName === 'DIV' || inputElem.tagName === 'SPAN'  || inputElem.tagName === 'P'){
                            if(is_exists) inputElem.style.display = 'block';
                            is_exists && is_exists != valueObj[key] ? inputElem.style.display = 'none' : (is_set ? inputElem.innerHTML = valueObj[key] : "");
                        }else if(inputElem.tagName === 'INPUT' || inputElem.tagName === 'SELECT'){
                            if(is_exists) inputElem.style.display = 'block';
                            is_exists && is_exists != valueObj[key] ? inputElem.style.display = 'none' : (is_set ? inputElem.value = valueObj[key] : "");
                        }else if(inputElem.tagName === 'IMG'){
                            if(is_exists) inputElem.style.display = 'block';
                            is_exists && is_exists != valueObj[key] ? inputElem.style.display = 'none' : (is_set ? inputElem.src = valueObj[key] : "");
                        }
                    }
                }
                DemoElem === null ? "" : MainCtrl.appendChild(ElemGroup);
            }
        }
    };

    // 返回定义的静态类
    var StaticClass = {
        description: 'This Method For Get Or Set The Input Value',
        /** [get 获取目标控件元素下的所有 Value 转换成 Object] */
        get: function (MainCtrl = null ,overflow = true){
            // 获取所有的主控件元素
            var ValElements = PrivateClass.getValElement(MainCtrl);
           
            var formData = new Object();
            for(var i=0;i < ValElements.length;i++){
                var ValElem = ValElements[i];

                if(ValElem.getAttribute('post-up') != undefined && ValElem.disabled === false){
                    // 可被重写的 KEY 值
                    var newKey = ValElem.getAttribute('set-data-name');
                    var key = newKey != undefined ? newKey.replace(/-/g ,'_') : ValElem.name.replace(/-/g ,'_');

                    // 赋值操作
                    if(overflow || formData[key] == undefined){
                        // 覆盖或原 KEY 不存在
                        formData[key] = ValElem.value;
                    }else{
                        // 多维数据类型判断
                        if(formData[key] instanceof Array){
                            formData[key].push(ValElem.value);
                        }else{
                            // 多维数据组合数据
                            var InterimArray = new Array();
                            InterimArray.push(formData[key]);
                            InterimArray.push(ValElem.value);

                            formData[key] = InterimArray;
                        }
                    }
                }
            }
            return formData;
        },

        /** [set 设置目标下的所有元素的值] */
        set: function (MainCtrl ,data ,overflow = true){
            // 获取主控件元素
            MainCtrl = PrivateClass.getDocElement(MainCtrl);

            if(data instanceof Array){
                // 数据 使用循环渲染
                return this.setGroup(MainCtrl[0] ,data ,overflow);
            }

            for(var i = 0;i < MainCtrl.length;i++){
                for(var key in data){
                    // 数据渲染
                    PrivateClass.rendering(MainCtrl[i] ,null ,[data])
                }
            }

            return MainCtrl;
        },
        /** [setGroup 设置目标下的所有元素的值] */
        setGroup: function (MainCtrl ,data ,overflow = true){
            // 获取渲染元素
            var DemoElem = MainCtrl.children[0].cloneNode(true);
            if(!DemoElem || DemoElem.length > 1) return false;
            overflow ? MainCtrl.innerHTML = "" : "";

            // 数据渲染
            PrivateClass.rendering(MainCtrl ,DemoElem ,data);
            return MainCtrl;
        },
    };



    return StaticClass;
})();